﻿using System;
using System.Collections.Generic;

namespace RuoTeng.Util
{
    public enum VersionCompare{
        Lt    = -1,
        Equal = 0,
        Gt    = 1,
    }
    class Version
    {
        public static readonly  string  version = "";
 
        public static string GetVersion()
        { 
            return version;
        }

        public static bool IsNewVersion(string cVersion, string nVersion)
        {
            return Compare(cVersion, nVersion, VersionCompare.Lt); 
        }

        public static bool Compare(string cVersion, string nVersion, VersionCompare Voperator)
        {
            if (cVersion == null) cVersion = "";
            if (nVersion == null) nVersion = "";
            string[] array_cVersion = cVersion.Split('.');
            string[] array_nVersion = nVersion.Split('.');
            int max = array_cVersion.Length > array_nVersion.Length ? array_cVersion.Length : array_nVersion.Length;
            long  __cVersion =0;   long  __nVersion =0;

            for (int i = 0; i < max; i++)
            {
                int __c = 0; int __n = 0; int __cL = 1; int __nL = 1;
                int __cPow =(max - i) - 1;
                int __nPow =(max - i) - 1;
                if (i < array_cVersion.Length && int.TryParse(array_cVersion[i], out __c))
                {
                    __cL = array_cVersion[i].Length;
                }
                if (i < array_nVersion.Length && int.TryParse(array_nVersion[i], out __n))
                {
                    __nL = array_nVersion[i].Length;
                }
                if (__cL > __nL)
                {
                    __nPow +=(__cL-__nL);
                }
                if (__cL < __nL)
                {
                    __cPow +=(__nL-__nL);
                }
                __cVersion += __c * (int)System.Math.Pow(10, __cPow);
                __nVersion += __n * (int)System.Math.Pow(10, __nPow);
            }
            switch (Voperator)
            {
                 case VersionCompare.Equal:
                        return  (__cVersion == __nVersion) ?  true  :  false;
                 case VersionCompare.Lt:
                        return (__cVersion  < __nVersion)  ?  true  :  false;
                 case VersionCompare.Gt:
                        return (__cVersion   > __nVersion) ?  true  :  false;
            }
            return false;
        }          
    }
}
